cmake_minimum_required(VERSION 3.1.0...3.30.3)
project(Taurus)

# Only generate Debug and Release configuration types.
set(CMAKE_CONFIGURATION_TYPES Debug Release RelWithDebInfo)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
SET(CMAKE_INSTALL_RPATH "$\{ORIGIN\}")

# Use folders in the resulting project files.
set_property(GLOBAL PROPERTY OS_FOLDERS ON)

if(${CMAKE_SIZEOF_VOID_P} EQUAL 4)
        SET(BUILD_ARCH "x86")
else()
        SET(BUILD_ARCH "x64")
endif()

if (UNIX AND NOT APPLE)
    set(LINUX 1)
elseif (WIN32)
    set(WINDOWS 1)
elseif (APPLE)
    set(MACOS 1)
else ()
    MESSAGE(STATUS "other platform")
endif ()


if(WINDOWS)
        set(QT_RUNTIME_DIR "D:/Qt5/5.15.2/msvc2019" CACHE PATH "qt 运行库路径" FORCE)
elseif(LINUX)
        set(QT_RUNTIME_DIR "/home/user/Qt/5.15.2/gcc_64" CACHE PATH "qt 运行库路径" FORCE)
elseif(MACOS)
endif()

set(CMAKE_PREFIX_PATH "${QT_RUNTIME_DIR}")

if(WINDOWS)
        set(CMAKE_PREFIX_PATH "${QT_RUNTIME_DIR}")
elseif(LINUX)
        set(Qt5_DIR "/home/user/Qt/5.15.2/gcc_64/lib/cmake/Qt5")
elseif(MACOS)
endif()


set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

if(WINDOWS)
        set(OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/output/${CMAKE_HOST_SYSTEM_NAME}/${BUILD_ARCH})
elseif(MACOS)
        set(OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/output/${CMAKE_HOST_SYSTEM_NAME}/${CMAKE_OSX_ARCHITECTURES})
else()
        set(OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/output/${CMAKE_HOST_SYSTEM_NAME})
endif()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${OUTPUT_DIR}/Debug)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${OUTPUT_DIR}/Release)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${OUTPUT_DIR}/RelWithDebInfo)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${OUTPUT_DIR}/Debug)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${OUTPUT_DIR}/Release)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO ${OUTPUT_DIR}/RelWithDebInfo)

if(NOT WINDOWS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++14 -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -g")
set(CMAKE_EXE_LINKER_FLAGS " -no-pie")
endif()

if (MACOS)
    set(CMAKE_EXE_LINKER_FLAGS "-framework Cocoa -framework AppKit -framework CoreData -framework Foundation")
    set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13") #Qt 5.15.15 是支持 macOS 10.12 及以上版本
endif ()


#创建输出文件夹
if(NOT CMAKE_CONFIGURATION_TYPES)
        list(APPEND CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE})
endif()


if (WINDOWS)
        add_compile_options(-D_UNICODE -DUNICODE)
elseif (LINUX)
	add_compile_options(-D_UNICODE -DUNICODE -DCCORE_LINUX)
elseif (MACOS)
        add_compile_options(-D_UNICODE -DUNICODE -DCCORE_MAC)
endif ()

if(WIN32)
        add_compile_options(/utf-8)
        add_compile_options(/wd4828)
endif()

find_package(Qt5 COMPONENTS Widgets)

macro(source_group_by_dir source_files)
    if(MSVC)
        set(sgbd_cur_dir ${CMAKE_CURRENT_SOURCE_DIR})
        foreach(sgbd_file ${${source_files}})
            string(REGEX REPLACE ${sgbd_cur_dir}/\(.*\) \\1 sgbd_fpath ${sgbd_file})
            string(REGEX REPLACE "\(.*\)/.*" \\1 sgbd_group_name ${sgbd_fpath})
            string(COMPARE EQUAL ${sgbd_fpath} ${sgbd_group_name} sgbd_nogroup)
            string(REPLACE "/" "\\" sgbd_group_name ${sgbd_group_name})
            if(sgbd_nogroup)
                set(sgbd_group_name "\\")
            endif(sgbd_nogroup)

            if(${sgbd_group_name} STREQUAL ".")
                set(sgbd_group_name "Source Files")
            endif()
            string(REPLACE ".\\" "" group_name "${sgbd_group_name}")

            source_group(${group_name} FILES ${sgbd_file})
        endforeach(sgbd_file)
    endif(MSVC)
endmacro(source_group_by_dir)

set(QCEFVIEW_DIR ${CMAKE_CURRENT_SOURCE_DIR}/source/widgets/cefview)
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
    add_library(QCefView SHARED IMPORTED)
    set_property(TARGET QCefView PROPERTY IMPORTED_LOCATION "${QCEFVIEW_DIR}/${CMAKE_BUILD_TYPE}/libQCefView.so")
    #set_property(TARGET QCefView PROPERTY IMPORTED_IMPLIB "${QCEFVIEW_DIR}/lib/QCefView.lib")
    set_property(TARGET QCefView PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${QCEFVIEW_DIR}/include")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
    add_library(QCefView SHARED IMPORTED)
    #set_property(TARGET QCefView PROPERTY IMPORTED_LOCATION "${QCEFVIEW_DIR}/bin/QCefView.dll")
    set_property(TARGET QCefView PROPERTY IMPORTED_LOCATION "${QCEFVIEW_DIR}")
    message(STATUS "${QCEFVIEW_DIR}/QCefView.lib")
    set_property(TARGET QCefView PROPERTY IMPORTED_IMPLIB "${QCEFVIEW_DIR}/QCefView.lib")
    set_property(TARGET QCefView PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${QCEFVIEW_DIR}/include")
else("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
    add_library(QCefView${CMAKE_OSX_ARCHITECTURES} SHARED IMPORTED)
    message(${CMAKE_OSX_ARCHITECTURES})
    set_property(TARGET QCefView${CMAKE_OSX_ARCHITECTURES} PROPERTY IMPORTED_LOCATION "${QCEFVIEW_DIR}/${CMAKE_BUILD_TYPE}/${CMAKE_OSX_ARCHITECTURES}/QCefView${CMAKE_OSX_ARCHITECTURES}")
    set_property(TARGET QCefView${CMAKE_OSX_ARCHITECTURES} PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${QCEFVIEW_DIR}/include")
endif()

add_subdirectory(source)
